package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Locale;


/**
 * Created by tony on 2018/5/8.
 */

public class TwoDatePickActivity extends Activity implements View.OnClickListener {
    private View rl_back;
    private TextView tv_title;
    private TextView tv_start_date, tv_completion_date;
    private EditText edt_start_date, edt_completion_date;
    private ImageView img_start_date, img_completion_date;
    private Button btn_save;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_two_date_pick);

        init();

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        btn_save = findViewById(R.id.btn_save);
        tv_start_date = findViewById(R.id.tv_start_date);
        tv_completion_date = findViewById(R.id.tv_completion_date);
        edt_start_date = findViewById(R.id.edt_start_date);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        img_start_date = findViewById(R.id.img_start_date);
        img_completion_date = findViewById(R.id.img_completion_date);

        tv_title.setText("日期选择");
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        edt_start_date.setOnClickListener(this);
        edt_completion_date.setOnClickListener(this);
        img_start_date.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
    }

    private void showStartDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_start_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();

    }

    private void showCompletionDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.edt_start_date:
                showStartDateDialog();
                break;
            case R.id.edt_completion_date:
                showCompletionDateDialog();
                break;
            case R.id.img_start_date:
                showStartDateDialog();
                break;
            case R.id.img_completion_date:
                showCompletionDateDialog();
                break;
            case R.id.btn_save:
               if (TextUtils.isEmpty(edt_start_date.getText().toString())||TextUtils.isEmpty(edt_completion_date.getText().toString())){
                   Toast.makeText(TwoDatePickActivity.this,"请选择日期",Toast.LENGTH_SHORT).show();
               }else{
                   Intent mIntent = new Intent();
                   mIntent.putExtra(ConstantUtil.KEY_1, edt_start_date.getText().toString().trim());
                   mIntent.putExtra(ConstantUtil.KEY_2, edt_completion_date.getText().toString().trim());
                   setResult(ConstantUtil.START_ACTIVITY_KEY_2, mIntent);
                   finish();
               }
                break;
        }
    }
}
